local servers = {
    -- @see more: https://github.com/neovim/nvim-lspconfig/tree/master/lua/lspconfig/server_configurations
    lua_ls = {
        settings = {
            Lua = {
                workspace = { checkThirdParty = false},
                telemetry = { enable = false },
            }
        }
    },
    vimls = {},
    pyright = {},
    docker_compose_language_service = {},
    -- phan = {},
    -- phpactor = {
    --     version = "2022.11.12",
        -- cmd = {
        --     "/Users/codezm/.local/share/phpactor-2022.11.12/bin/phpactor",
        --     "language-server"
        -- }
        -- document_config = {
        --     default_config = {
        --         cmd = {
        --             "/Users/codezm/.local/share/phpactor-2022.11.12/bin/phpactor",
        --             "language-server"
        --         }
        --     }
        -- }
    -- },
}
local on_attach = function(_, bufnr)
    -- bufnr: buffer number 
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc'
    local nmap = function (keys, func, desc)
        if desc then
            desc = 'LSP: ' .. desc
        end
        vim.keymap.set('n', keys, func, {buffer = bufnr, desc = desc})
    end

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    -- local opts = { buffer = ev.buf }
    --
    nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
    nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
    -- nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
    nmap('gr', vim.lsp.buf.references, '[G]oto [R]eferences')
    nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
    nmap('C-k', vim.lsp.buf.signature_help, 'Signature Documentation')
    nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
    nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
    nmap('<space>wl', function()
        print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, '[W]orkspace [L]ist Folders')
    nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
    nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
    nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
    nmap('<space>fc', function()
        vim.lsp.buf.format { async = true }
    end, '[F]ormat code')
    nmap('gi', vim.lsp.buf.implementation, '[G]oto [I]mplementation')

    -- vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    -- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    -- vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    -- vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
    -- vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
    -- vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
    -- vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
    -- vim.keymap.set('n', '<space>wl', function()
    --     print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    -- end, opts)
    -- vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
    -- vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
    -- vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
    -- vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    -- vim.keymap.set('n', '<space>f', function()
    --     vim.lsp.buf.format { async = true }
    -- end, opts, "")
end


require("neodev").setup()
require("mason").setup({
  ui = {
      icons = {
          package_installed = "✓",
          package_pending = "➜",
          package_uninstalled = "✗"
      }
  }
})

-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
-- vim.keymap.set('n', '<space>e', vim.diagnostic.open_float)
-- vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
-- vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
-- vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist)

local capabilities = require('cmp_nvim_lsp').default_capabilities()
require("mason-lspconfig").setup({
  -- 确保安装，根据需要填写
    -- ensure_installed = {
    --     "lua_ls",
    -- },
    ensure_installed = vim.tbl_keys(servers),
    -- handlers = {
    --     function(server_name)
    --         require("lspconfig")[server_name].setup {
    --             settings = servers[server_name],
    --             on_attach = on_attach,
    --             capabilities = capabilities,
    --         }
    --     end
    -- }
})

for server, config in pairs(servers) do
    require("lspconfig")[server].setup(
        vim.tbl_deep_extend("keep", {
            on_attach = on_attach,
            capabilities = capabilities,
        }, config)
    )
end

-- require("lspconfig").phan.setup ({
--     
-- })

require("lspconfig").phpactor.setup ({
    cmd= {"/usr/local/bin/phpactor", "language-server"},
    on_attach = on_attach,
    capabilities = capabilities,
   -- docs = {
   --     default_config = {
   --         cmd = {
   --             "/Users/codezm/.local/share/phpactor-2022.11.12/bin/phpactor",
   --             "language-server"
   --         }
   --     }
   -- }
})



